package com.itheima.userservice.controller;

import com.itheima.userservice.bean.UserBean;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/MongoDB/use")
@Api("s使用MongoDB")
public class MongoDBController {

    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoTemplate MongoDBController(MongoTemplate template){
        return this.mongoTemplate = template;
    }

    @PostMapping("/add")
    public Map<String,Object> addMongoDB(@RequestBody UserBean userBean){
        UserBean insert = mongoTemplate.insert(userBean);
        System.out.println(insert);
        return null;
    }

    @PostMapping("/get")
    public Map<String,Object> getMongoDB(){
        //获取集合(也就是上面存进去的类型)
        Set<String> collectionNames = mongoTemplate.getCollectionNames();
        List<UserBean> all = mongoTemplate.findAll(UserBean.class);

        Query query1 = new Query();

        //查询所有的User集合的文档
        List<UserBean> objects = mongoTemplate.find(query1,UserBean.class,"User");
        //根据id查询,需要注意:
        //1、对象的id设置必须是String类型
        //2、如果前端传递进来是没有id的情况,会生成一个未知规则的id;如果传递进来有id则只会使用这个传递的id
        UserBean byId = mongoTemplate.findById("64f47374b60d347a40c652f4", UserBean.class);
        return null;
    }

    @PostMapping("/delete")
    public Map<String,Object> delete(){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in("4"));
        DeleteResult remove = mongoTemplate.remove(query, UserBean.class);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("remove",remove);
        return hashMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in("4"));
        UserBean userBean = mongoTemplate.findById("4", UserBean.class);
        Update update = new Update();
        update.set("name","野兽后辈");
        //update.set("phone","12345678990");

        UpdateResult upsert = mongoTemplate.upsert(query, update, UserBean.class);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("upsert",upsert);
        hashMap.put("count",upsert.getModifiedCount());
        return hashMap;
    }

    @PostMapping("/limit")
    public Map<String,Object> limit(){
        Query query = new Query();
        query.limit(2);
        List<UserBean> userBeans = mongoTemplate.find(query, UserBean.class);
        return null;
    }
}
